Learn T-SQL Commands with Samples
Skip Navigation Links
Skip Navigation Links.
Expand DatabaseDatabase
Expand TableTable
Expand ViewView
Expand Stored ProcedureStored Procedure
Expand Data FilteringData Filtering
Expand Data GroupingData Grouping
Expand JoinsJoins
Expand TriggerTrigger
Expand CursorCursor
Expand OperatorsOperators
Expand ConstraintsConstraints
Collapse FunctionsFunctions
Expand Conditional ProcessingConditional Processing
Expand LoopingLooping
Expand Error HandlingError Handling
Expand v.IMP Queriesv.IMP Queries
Expand XMLXML
Expand Query PerformanceQuery Performance
Expand QueriesQueries
Expand NormalizationNormalization
Expand CreateCreate
     

Date and Time Functions

 
SELECT

      GETDATE()         as	'GetDate',
      DAY(GETDATE())    as	'Day',
      MONTH(GETDATE())  as	'Month',
      YEAR(GETDATE())   as	'Year',
      GETUTCDATE()      as	'UTC',

      DATEADD(DAY,1,  GETDATE())    as	'DateAdd',
      DATENAME(MONTH, GETDATE())    as	'DateName',
      DATEPART(WEEK,  GETDATE())    as	'DatePart',

      DATEDIFF(YEAR,'05-30-1980',GETDATE()) as	'DateDiff'
    
      
Output
GetDate Day Month Year UTC DateAdd DateName DatePart DateDiff
2013-06-06 12:02:51.727 6 6 2013 2013-06-06 16:02:51.727 2013-06-07 12:02:51.727 June 23 33